//
//  MHHomeBanner.m
//  ManHua
//
//  Created by 赵永泉 on 15/11/2.
//  Copyright © 2015年 蓝欧科技. All rights reserved.
//

#import "MHHomeBanner.h"
#import "MHDataManager.h"
#import <AFNetworking.h>
#define kResultURL @"http://112.124.96.190:9090/bukamanhua5appjobs/ComicHandle.ashx"

@implementation MHHomeBanner

- (void)setValue:(id)value forKey:(NSString *)key
{
    [super setValue:value forKey:key];
    if ([key isEqualToString:@"image_set_id"]) {
        NSLog(@"%@", value);
        [[AFHTTPRequestOperationManager manager] GET:[NSString stringWithFormat:@"https://pi.comikon.net/imagegroup/%@/autopick/", value] parameters:[NSDictionary dictionaryWithObjects:@[@"json", @"440", @"both", @"640"] forKeys:@[@"format", @"height", @"match", @"width"]] success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
            self.BookIconOtherURL = responseObject[@"image_url"];
            self.imageSize = CGSizeMake([responseObject[@"width"] floatValue], [responseObject[@"height"] floatValue]);
            [[NSNotificationCenter defaultCenter]postNotificationName:@"imageUrl" object:nil userInfo:[NSDictionary dictionaryWithObjects:@[self.BookIconOtherURL, [NSNumber numberWithInteger:self.seq]] forKeys:@[@"url", @"index"]]];
            [[MHDataManager shareManager] updateImage:self.BookIconOtherURL homeBanner:self];
        } failure:^(AFHTTPRequestOperation * _Nonnull operation, NSError * _Nonnull error) {
            NSLog(@"%@", error.localizedDescription);
        }];
    }
    if ([key isEqualToString:@"objects"]) {
        self.meta_id = [value[@"meta_id"] integerValue];
    }
}

- (void)acquireIntroductDataWithName:(NSString *)string
{
    NSLog(@"%@", string);
    NSString *name = [NSString stringWithCString:[string UTF8String] encoding:NSUTF8StringEncoding];
    NSDictionary *dict = [NSDictionary dictionaryWithObjects:@[name,@"search",@"0"] forKeys:@[@"name",@"method",@"type"]];
    [[AFHTTPRequestOperationManager manager] GET:kResultURL parameters:dict success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        NSArray *arr = responseObject;
        if (arr.count == 0) {
            [[NSNotificationCenter defaultCenter]postNotificationName:@"cannotSearch" object:nil userInfo:[NSDictionary dictionaryWithObject:[NSNumber numberWithInteger:self.seq] forKey:@"seq"]];
            MHComicIntroduct *model = [[MHComicIntroduct alloc]init];
            [[MHDataManager shareManager]insertValueToTabel:@"MHMHHomeBanner" homeBanner:self];

            model.BookID = self.meta_id;
            self.comicIntroduct = model;
            return;
        }
        MHComicIntroduct *model = [[MHComicIntroduct alloc]init];
        [model setValuesForKeysWithDictionary:arr[0]];
        self.comicIntroduct = model;
        [[MHDataManager shareManager]insertValueToTabel:@"MHMHHomeBanner" homeBanner:self];

    } failure:^(AFHTTPRequestOperation * _Nonnull operation, NSError * _Nonnull error) {
        NSLog(@"%@", error.localizedDescription);
    }];
    
    
}

@end
